//==============================================================================
//
// File: AddFavorite.cpp
// Project: Browser
// Author(s): Matthias Van Eeghem (matthias@van-eeghem.com)
// License: See LICENSE in root directory
//
// The AddFavorite dialog allows a user to add a server to the favorites list.
//
//==============================================================================

#include "Constants.h"
#include "AddFavoriteDialog.h"
#include "ui_AddFavoriteDialog.h"
#include "CFavoriteList.h"

// The constructor
AddFavorite::AddFavorite(QWidget * parent, MainWindow * mainWindow) : QDialog(parent), ui(new Ui::AddFavorite)
{
    ui->setupUi(this);

    m_pMainWindow = mainWindow;

    // Disable the icon and close button, looks nicer
    setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint);
}

// Called when the "OK" button is clicked
void AddFavorite::on_okButton_clicked()
{
    // Get the entered IP
    QString ServerIP = ui->lineEdit->text();
    QStringList ServerAndPort = ServerIP.split(':');

    if(CFavoriteList::DoesFavoriteExist(ServerIP))
    {
        QMessageBox::information(m_pMainWindow, "Invalid IP", "That IP address has already been added to your favorites list.");
        return;
    }

    // Make sure the IP the user tries to add is in the ip:port format
    if(ServerAndPort.length() < 2)
    {
        QMessageBox::information(this, "Invalid IP", "The IP you entered was invalid, the correct format is ip:port.");
        return;
    }

    // Save it to the registry and add the IP to the listview
    CFavoriteList::AddFavorite(ServerIP, true);

    // Request the information for the server
    m_pMainWindow->m_pNetworkManager->get(QNetworkRequest(QUrl(QString(MASTERLIST_HOST) + QString(SERVERINFO_PATH) + ServerIP)));
	
    close();
}

// Called when the "Cancel" button is clicked
void AddFavorite::on_cancelButton_clicked()
{
    close();
}

// Destructor
AddFavorite::~AddFavorite()
{
    delete ui;
}
